#include "HttpCmdProcess.h"
#include "CurtainActManager.h"
#include "FileTransfer.h"
#include "WlanService.h"
#include "ScreenMirrorStream.h"
#include "HttpDeviceInfoManager.h"
#include "StreamerCurtainPlay.h"

namespace Media{
	HttpCmdProcess*          HttpCmdProcess::sm_objectIns;
	std::mutex 		         HttpCmdProcess::sm_insMutex;

	HttpCmdProcess* HttpCmdProcess::instance()
	{
		if(sm_objectIns == nullptr)
		{
			std::lock_guard<std::mutex> guard(sm_insMutex);
			if(sm_objectIns == nullptr)
			{
				sm_objectIns = new HttpCmdProcess();
			}
		}
		return sm_objectIns;
	}

	HttpCmdProcess::HttpCmdProcess() 
	{ 

	}

	HttpCmdProcess::~HttpCmdProcess() 
	{ 

	}

	bool HttpCmdProcess::getDeviceCapacity(Json::Value &table)
	{
		return CurtainActManager::instance()->getDeviceCapacity(table);
	}

	bool HttpCmdProcess::getStreamType(Json::Value &table)
	{    
		return CurtainActManager::instance()->getStreamType(table);
	}

	bool HttpCmdProcess::setCurtainLayout(const Json::Value &table)
	{
		//printf("setCurtainLayout table=%s\n",table.toStyledString().c_str());

		return CurtainActManager::instance()->setCurtainLayout(table);
	}

	bool HttpCmdProcess::getCurtainLayout(Json::Value &table)
	{
		//printf("setCurtainLayout table=%s\n",table.toStyledString().c_str());

		return CurtainActManager::instance()->getCurtainLayout(table);
	}

	bool HttpCmdProcess::setPlayStrategy(const Json::Value &table)
	{
		//printf("setPlayStrategy table=%s\n",table.toStyledString().c_str());

		if((!table.isMember("Number")) || (!table.isMember("PlayMode"))
        || (!table.isMember("Action")))
        {
            errorf("Json params is error\n");
            return false;
        }
        
        int num = table["Number"].asInt();
        int action = table["Action"].asInt();
        
    	///数字标牌播放处理
    	if(num == StreamerNum)
    	{
    	    if(action == 0)
    	    {
                return StreamerCurtainPlay::instance()->stopStreamerCurtain();
    	    }
    		return StreamerCurtainPlay::instance()->playStreamerCurtain();
    	}

		return CurtainActManager::instance()->setPlayStrategy(table);
	}

	bool HttpCmdProcess::playDefaultCurtain()
	{
		//printf("setPlayStrategy table=%s\n",table.toStyledString().c_str());

		return CurtainActManager::instance()->playDefaultCurtain();
	}
	

	bool HttpCmdProcess::SetDeviceShareStatus(const Json::Value &table)
	{
		//printf("setPlayStrategy table=%s\n",table.toStyledString().c_str());

		return CurtainActManager::instance()->SetDeviceShareStatus(table);
	}

	bool HttpCmdProcess::fileStreamReset(string filename, string Type){
		return FileTransfer::instance()->fileStreamReset(filename, Type);
	}

	bool HttpCmdProcess::streamWrite(string filename, const void *buffer, size_t size){
		return FileTransfer::instance()->streamWrite(filename, buffer, size);
	}

	bool HttpCmdProcess::fileVerify(string filename, const char *md5){
		bool ret =  FileTransfer::instance()->fileVerify(filename, md5);
		return ret;
	}

	bool HttpCmdProcess::GetFileInfo(Json::Value table, string &filename, unsigned int &size, string &md5){
		return FileTransfer::instance()->GetFileInfo(table, filename, size, md5);
	}

	bool HttpCmdProcess::CloudUploadStream(Json::Value table, function<bool(string file, string url, function<void(const SockException &ex)> cb)> callback){
		return FileTransfer::instance()->CloudUploadStream(table, callback);
	}

	int HttpCmdProcess::getCloudUpMark(){
		return FileTransfer::instance()->getCloudUpMark();
	}
			
	bool HttpCmdProcess::setCloudUpMark(int mark){
		return FileTransfer::instance()->setCloudUpMark(mark);
	}

	bool HttpCmdProcess::scanAP(Json::Value &apList){
		return WlanService::instance() -> scanAP(apList);
	}

	bool HttpCmdProcess::ConnectWifi(const Json::Value table)
	{	
		if(!table.isMember("SSID") || !table.isMember("key")){
			return false;
		}

		bool iRet = WlanService::instance() -> connectWifi(table["SSID"].asString(), table["key"].asString());

		//CurtainActManager::instance()->playStartUpCurtain();
		
		return iRet;
	}


	bool HttpCmdProcess::openApService(const Json::Value table)
	{	
		if(!table.isMember("Password") || !table.isMember("FreqBand") || !table.isMember("Retransmit")){
			return false;
		}
		return WlanService::instance() -> openApService(table["Password"].asString(), table["FreqBand"].asString(), table["Retransmit"].asInt(), "");
	}

	bool HttpCmdProcess::getOutputInterface(Json::Value &table)
	{
		return CurtainActManager::instance()->getOutputInterface(table);
	}

	bool HttpCmdProcess::getMirrorPinCode(std::string &pinCode)

	{
		return ScreenMirrorStream::instance()->getMirrorCode(pinCode);
	}

	bool HttpCmdProcess::SetNetwork(Json::Value table){
		if(!table.isMember("Dhcp") || !table.isMember("IP") || !table.isMember("GateWay") || !table.isMember("Mask")){
			return false;
		}
		if(0 != mii_diag("eth0")){
			return false;
		}
		auto lam = [table](){
			WlanService::instance()->setWiredNetwork(table["IP"].asString(), table["Mask"].asString(), table["GateWay"].asString(), table["Dhcp"].asBool());
		};
		thread syncAlocIp(lam);
		syncAlocIp.detach();
		return true;
	}
		
	bool HttpCmdProcess::GetNetwork(Json::Value &table){
		return WlanService::instance() -> getWiredNetwork(table);		
	}

	bool HttpCmdProcess::Subscribe(function<bool(Json::Value table)> cb){
		return HttpDeviceInfoManager::instance()->Subscribe(cb);
	}


	bool HttpCmdProcess::GetDeviceList(Json::Value &table){
		return HttpDeviceInfoManager::instance()->GetDeviceList(table);
	}

}

